package com.ny.zmb.openapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.openapi.entity.PmUserWallet;
import com.ny.zmb.openapi.entity.PmUserWalletLog;
import com.ny.zmb.openapi.mapper.PmUserWalletLogMapper;
import com.ny.zmb.openapi.mapper.PmUserWalletMapper;
import com.ny.zmb.openapi.mapper.merchant.MerchantWithdrawMapper;
import com.ny.zmb.openapi.service.PmUserWalletLogService;
import com.ny.zmb.openapi.ysutil.from.merchant.MerchantWithdraw;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class PmUserWalletLogServiceImpl extends ServiceImpl<PmUserWalletLogMapper, PmUserWalletLog> implements PmUserWalletLogService {


    @Resource
    private PmUserWalletMapper pmUserWalletMapper;
    @Resource
    private PmUserWalletLogMapper pmUserWalletLogMapper;
    @Resource
    private MerchantWithdrawMapper merchantWithdrawMapper;

    /**
     * 增加添加余额明细
     *
     * @param log
     * @return
     */
    @Override
    public Boolean addWalletLog(PmUserWalletLog log) {
        LambdaQueryWrapper<PmUserWallet> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PmUserWallet::getUserId, log.getUserId());
        PmUserWallet wallet = this.pmUserWalletMapper.selectOne(wrapper);
        if (null == wallet) {
            throw new RuntimeException("您还未注册钱包，请先注册！");
        }
        LambdaQueryWrapper<PmUserWalletLog> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PmUserWalletLog::getUserId, log.getUserId());
        queryWrapper.orderByDesc(PmUserWalletLog::getCreatetime);
        queryWrapper.last("limit 1");
        queryWrapper.eq(PmUserWalletLog::getMoneyType,log.getMoneyType());
        PmUserWalletLog pmUserWalletLog = this.baseMapper.selectOne(queryWrapper);

        Long l = System.currentTimeMillis();
        l = l / 1000;
        log.setCreatetime(l.intValue());
        if (null == pmUserWalletLog) {
            log.setBeforeMoney(BigDecimal.ZERO);
            log.setAfterMoney(log.getMoney());
        } else {
            BigDecimal afterMoney = pmUserWalletLog.getAfterMoney();
            log.setBeforeMoney(afterMoney);
            log.setAfterMoney(afterMoney.add(log.getMoney()));
        }
        int insert = this.baseMapper.insert(log);
        return insert > 0;
    }

    @Override
    public Boolean addMerchantWalletLog(PmUserWalletLog log) {
        int insert = this.baseMapper.insert(log);
        return insert > 0;
    }


    /**
     * 根据订单号查询提现订单
     *
     * @param requestNo
     * @return
     */
    @Override
    public PmUserWalletLog getByRequestNo(String requestNo) {
        LambdaQueryWrapper<PmUserWalletLog> pmUserWalletLogLambdaQueryWrapper = Wrappers.lambdaQuery();
        pmUserWalletLogLambdaQueryWrapper.eq(PmUserWalletLog::getTargetId, requestNo);
        PmUserWalletLog pmUserWalletLog = pmUserWalletLogMapper.selectOne(pmUserWalletLogLambdaQueryWrapper);
        return pmUserWalletLog;
    }

}
